; Basic initialization of an NES

; Some NES ports
export port ppuControl = $2000
export port ppuMask = $2001
export port ppuStatus = $2002
export port apuDmcFrequencey = $4010
export port oamDma = $4014
export port apuFrameCounter = $4017

export sub main
	; Set up registers
	apuFrameCounter = $40
	apuDmcFrequencey = 0
	ppuControl = 0
	ppuMask = 0
	
	; Assembly block to clear RAM and wait for PPU warmup
	asm
		bit	ppuStatus
		ppuWait1:
			bit	ppuStatus
			bpl	ppuWait1
		ldx #$00
		loop:
			lda #$ff	; Page $02 needs to be cleared to $FF to force OAM data
			sta	$0200,x	; off-screen. Change this page if you don't use $02 for
			lda #$00	; the OAM DMA buffer.
			sta $0000,x
			sta $0100,x
			sta $0300,x
			sta $0400,x
			sta $0500,x
			sta $0600,x
			sta $0700,x
			dex
			bne	loop
		ppuWait2:
			bit	ppuStatus
			bpl	ppuWait2
	end asm
	
	; Enable rendering
	ppuControl = %1000000
	ppuMask = %00011110
	
	; Program loop
	loop
	end loop
end sub

export sub frame
	; OAM DMA, takes about 513 cycles
	oamDma = $02
end sub

export sub interrupt
end sub
